#!/bin/bash

DAVFS_CFG="/etc/davfs2/davfs2.conf"
DAVFS_SEC="/etc/davfs2/secrets"
CFG_DIR="/etc/autobackup"

MPOINT="/mnt/cloudfiles"
CLOGIN=""
CPASSWORD=""
HOST=`hostname`

yum install kernel kernel-devel fuse dkms dkms-fuse davfs2 pbzip2

sed -i 's/# use_locks       1/use_locks 0/g' ${DAVFS_CFG}

echo -n "Enter mount point [/mnt/cloudfiles]: "
read ANSVER
[[ -n "${ANSVER}" ]] &&  MPOINT="${ANSVER}"
[[ -d "${MPOINT}" ]] || mkdir -p "${MPOINT}"

echo -n "OpSource Cloud Files login: "
read ANSVER
[[ -n "${ANSVER}" ]] && CLOGIN=${ANSVER};

echo -n "OpSource Cloud Files password: " 
read ANSVER
[[ -n "${ANSVER}" ]] && CPASSWORD=${ANSVER};

echo "${MPOINT} ${CLOGIN} ${CPASSWORD}" >> ${DAVFS_SEC}
echo "https://cf-na-east-01.opsourcecloud.net/dav ${MPOINT} davfs rw,users,noauto 0 0" >> /etc/fstab

echo "Try to mount...."
mount ${MPOINT}
RES=`cat /etc/mtab | grep ${MPOINT}`
echo "Done: ${RES}"

echo "Copy files..."
[[ -d "${CFG_DIR}" ]] || mkdir -p "${CFG_DIR}"
cp ./autobackup.conf-clear ${CFG_DIR}/autobackup.conf
[[ -d "/usr/local/bin" ]] || mkdir -p "/usr/local/bin"
cp bin/* /usr/local/bin/

echo "1  3  *  *  * root    /bin/nice /usr/local/bin/autobackup.sh > /dev/null 2>&1" >> /etc/crontab

exit 0